package com.xunce.xcworkservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xunce.xcworkservice.Enums.Enums;
import com.xunce.xcworkservice.dao.RoleMenuMapper;
import com.xunce.xcworkservice.entity.AccountRoles;
import com.xunce.xcworkservice.entity.Menus;
import com.xunce.xcworkservice.entity.RoleMenu;
import com.xunce.xcworkservice.service.AccountRolesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunce.xcworkservice.dao.RolesMapper;
import com.xunce.xcworkservice.entity.Roles;
import com.xunce.xcworkservice.service.RolesService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements RolesService{

    private final AccountRolesService accountRolesService;

    private final RoleMenuMapper roleMenuMapper;

    @Override
    public List<Roles> getList(boolean permission) {
        List<Roles> roles = list();
        if (permission){
            fillPermission(roles);
        }
        return roles;
    }

    @Override
    public boolean edit(Roles roles) {
        if (roles.getRoleId() == null){
            roles.setState(Enums.RoleState.ENABLED.getValue());
            return save(roles);
        }
        return updateById(roles);
    }

    @Override
    public boolean setSate(Integer id, Integer state) {
        UpdateWrapper<Roles> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Roles::getRoleId, id)
                .set(Roles::getState, state);
        return update(wrapper);
    }

    @Override
    public boolean checkDelete(Integer id) {

        QueryWrapper<AccountRoles> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AccountRoles::getRoleId, id);
        long count = accountRolesService.count(wrapper);
        if (count > 0){
            throw new RuntimeException("当前角色正在使用中, 不可删除");
        }
        return removeById(id);
    }

    @Override
    @Transactional
    public boolean rolePermissionEdit(Integer id, List<Integer> permission) {
        try {
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(RoleMenu::getRoleId, id);
            roleMenuMapper.delete(queryWrapper);
            if (permission != null){
                List<RoleMenu> roleMenus = new ArrayList<>();
                for (Integer menuId : permission){
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(id);
                    roleMenu.setMenuId(menuId);
                    roleMenus.add(roleMenu);
                }
                roleMenuMapper.batchInsert(roleMenus);
            }
            return true;
        } catch (Exception e){
            log.error("rolePermissionEdit_err", e);
            return false;
        }
    }

    private void fillPermission(List<Roles> roles){
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(null);
        Map<Integer, List<Integer>> roleMenuMap = roleMenus.stream().collect(Collectors.groupingBy(RoleMenu::getRoleId, Collectors.mapping(RoleMenu::getMenuId, Collectors.toList())));
        for (Roles roles1 : roles){
            List<Integer> menusList = roleMenuMap.get(roles1.getRoleId());
            if (menusList != null){
                roles1.setPermissions(menusList.stream()
                        .map(Object::toString)
                        .collect(Collectors.joining(",")));
            }

        }
    }
}
